//
//  QSUserAccount.m
//  SinaWeiBo
//
//  Created by apple on 16/6/1.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "QSUserAccount.h"

@interface QSUserAccount()<NSCoding>

@end

@implementation QSUserAccount
/**
 *  重写expires_in的set方法
 *
 *  @param expires_in access_token的生命周期，单位是秒数
 */
- (void)setExpires_in:(NSTimeInterval)expires_in
{
    _expires_in = expires_in;
    _expires_date = [NSDate dateWithTimeIntervalSinceNow:expires_in];
}

/**
 *  单例
 */
singleton_implementation(QSUserAccount)

/**
 *  字典转模型
 *
 *  @param dict 传入的字典
 */
- (instancetype)initWithDict:(NSDictionary *)dict
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    
    return self;
}
+ (instancetype)userAccountWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}

// 因为并没有保存所有的数据，所以要重写setValue: forUndefinedKey:
/**
 *  重写setValue: forUndefinedKey:
 */
- (void)setValue:(id)value forUndefinedKey:(NSString *)key
{}

/**
 *  修改日志的输出信息，重写description方法
 */
- (NSString *)description
{
    NSArray *keysArray = @[@"access_token", @"expires_in", @"uid", @"avatar_large", @"name", @"expires_date"];
    return [[self dictionaryWithValuesForKeys:keysArray] description];
}

#pragma mark - 用户数据归档和解归档
/**
 *  将用户数据保存到沙盒(归档)
 */
- (void)saveAccount
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"account.plist"];
    
    [NSKeyedArchiver archiveRootObject:self toFile:path];
    QSLog(@"%@", path);
}

/**
 *  读取沙盒中的数据（解归档）
 */
- (QSUserAccount *)loadAccount
{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"account.plist"];
    
    QSUserAccount *account = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    
    // 判断用户的token是否过期
    if ([account.expires_date compare:[NSDate date]] == NSOrderedDescending) {
        return account;
    }
    
    return nil;
}

#pragma mark - 实现NSCoding协议的方法
/**
 *  归档
 */
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.access_token forKey:@"access_token"];
    [aCoder encodeDouble:self.expires_in forKey:@"expires_in"];
    [aCoder encodeObject:self.uid forKey:@"uid"];
    [aCoder encodeObject:self.avatar_large forKey:@"avatar_large"];
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.expires_date forKey:@"expires_date"];
}

/**
 *  解归档
 */
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];
    self.access_token = [aDecoder decodeObjectForKey:@"access_token"];
    self.expires_in = [aDecoder decodeDoubleForKey:@"expires_in"];
    self.uid = [aDecoder decodeObjectForKey:@"uid"];
    self.avatar_large = [aDecoder decodeObjectForKey:@"avatar_large"];
    self.name = [aDecoder decodeObjectForKey:@"name"];
    self.expires_date = [aDecoder decodeObjectForKey:@"expires_date"];
    
    return self;
}
@end
